package list;


public class MyArrayList {
    public String[] data;
    public int size;
    public MyArrayList(){
        data = new String[10];
        size = 0;
    }

    //尾插
    public void add(String elem){
        if (size == data.length){
            //进行扩容操作
            resize();
        }
        data[size] = elem;
        size++;
    }
    //指定位置插入
    public void add(int index, String elem){
        //判断合法性
        if (index < 0 || index > size){
            throw new RuntimeException("下标超出异常");
        }
        //判断是否需要扩容
        if (size == data.length){
            resize();
        }
        //1.将下标 index 后面的数据往后移动
        for (int i = size - 1; i >= index; i--){
            data[i + 1] = data[i];
        }
        //2.插入到index位置
        data[index] = elem;
        size++;
    }
    //删除指定下标元素
    public String remove(int index){
        //判断下标合法性
        if (index < 0 || index >= size){
            throw new RuntimeException("下标超出异常");
        }
        String elem = data[index];
        //删除操作
        for (int i = index; i < size - 1; i++){
            data[i] = data[i + 1];
        }
        size--;
        return elem;
    }
    //删除指定元素
    public boolean remove(String elem){
        int removePos = 0;
        //遍历顺序表,找到下标为 elem 的元素
        for (removePos = 0; removePos < size; removePos++){
            if(data[removePos].equals(elem)){
                break;
            }
        }
        //两种情况 1.找到了  2.未找到
        if (removePos == size){
            return false;
        }
        remove(removePos);
        return true;
    }

    public int indexOf(String elem){
        int i = 0;
        //遍历顺序表
        for (i = 0; i < size; i++){
            if (data[i].equals(elem)){
                return i;
            }
        }
        return -1;
    }
    public boolean contain(String elem){
        int i = 0;
        //遍历顺序表
        for (i = 0; i < size; i++){
            if (data[i].equals(elem)){
                return true;
            }
        }
        return false;
    }
    public MyArrayList sublist(int fromIndex, int toIndex){
        //判断合法性
        if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex){
            throw new RuntimeException("下标异常");
        }
        MyArrayList sublist = new MyArrayList(toIndex - fromIndex);
        for (int i = fromIndex; i <= toIndex; i++){
            sublist.add(data[i]);
        }
        return sublist;
    }
    //清空顺序表
    public void clean(){
        size = 0;
    }

    public void resize(){
        //1.创建容量更大的数组
        String[] newData = new String[(int)(data.length * 1.5)];
        //2.将数据移动到新数组中
        for (int i = 0; i < data.length; i++){
            newData[i] = data[i];
        }
        //3.新数组代替旧数组
        data = newData;
    }

    public MyArrayList(int capacity){
        if (capacity <= 10){
            capacity = 10;
        }
        data = new String[capacity];
        size = 0;
    }
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < size; i++){
            stringBuilder.append(data[i]);
            if (i < size - 1){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
